package org.dromara.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.app.appParams.AppGuiZeParams;
import org.dromara.app.appReturn.AppProtocolRulesReturn;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.SkProtocolRulesBo;
import org.dromara.app.domain.vo.SkProtocolRulesVo;
import org.dromara.app.domain.SkProtocolRules;
import org.dromara.app.mapper.SkProtocolRulesMapper;
import org.dromara.app.service.ISkProtocolRulesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 协议规则Service业务层处理
 *
 * @author Evacuation
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
public class SkProtocolRulesServiceImpl implements ISkProtocolRulesService {

    private final SkProtocolRulesMapper baseMapper;

    /**
     * 查询协议规则
     *
     * @param id 主键
     * @return 协议规则
     */
    @Override
    public SkProtocolRulesVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询协议规则列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 协议规则分页列表
     */
    @Override
    public TableDataInfo<SkProtocolRulesVo> queryPageList(SkProtocolRulesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkProtocolRules> lqw = buildQueryWrapper(bo);
        Page<SkProtocolRulesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的协议规则列表
     *
     * @param bo 查询条件
     * @return 协议规则列表
     */
    @Override
    public List<SkProtocolRulesVo> queryList(SkProtocolRulesBo bo) {
        LambdaQueryWrapper<SkProtocolRules> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkProtocolRules> buildQueryWrapper(SkProtocolRulesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkProtocolRules> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, SkProtocolRules::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), SkProtocolRules::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SkProtocolRules::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增协议规则
     *
     * @param bo 协议规则
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SkProtocolRulesBo bo) {
        SkProtocolRules add = MapstructUtils.convert(bo, SkProtocolRules.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改协议规则
     *
     * @param bo 协议规则
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SkProtocolRulesBo bo) {
        SkProtocolRules update = MapstructUtils.convert(bo, SkProtocolRules.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkProtocolRules entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除协议规则信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 规则相关接口
     *
     * @param app
     */
    @Override
    public AppProtocolRulesReturn protocolRules(AppGuiZeParams app) {
        AppProtocolRulesReturn rulesReturn = new AppProtocolRulesReturn();
        SkProtocolRules sk = baseMapper.selectOne(
            new QueryWrapper<SkProtocolRules>().lambda()
                .eq(SkProtocolRules::getType, app.getType())
                .last("limit 1")
        );

        if (sk != null) {
            rulesReturn = BeanUtil.copyProperties(sk,AppProtocolRulesReturn.class);
        }
        return rulesReturn;
    }
}
